package com.wiken.storeservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.xml.internal.bind.v2.TODO;
import com.wiken.servicecommon.common.Code;
import com.wiken.servicecommon.common.PageUtill;
import com.wiken.servicecommon.common.R;
import com.wiken.servicecommon.common.TokenUtil;
import com.wiken.servicecommon.common.exception.DefaultException;
import com.wiken.storeservice.entity.Commodity;
import com.wiken.storeservice.entity.StoreCategories;
import com.wiken.storeservice.service.impl.CommodityServiceImpl;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wiken
 * @since 2022-12-28
 */
@RestController
@RequestMapping("/storeservice/commodity")

public class CommodityController {

    @Autowired
    CommodityServiceImpl commodityService;

    @PostMapping("addCommodity")
    public R addCommodity(@RequestBody Commodity commodity, @RequestHeader("Authorization") String token){


        boolean success = commodityService.addCommodity(commodity, token);

        if(!success) throw new DefaultException(Code.ERROR, "添加商品失败");

        return R.ok().code(Code.SUCCESS).message("添加商品成功");
    }

    @PutMapping("updateCommodity")
    public R updateCommodity(@RequestBody  Commodity commodity, @RequestHeader("Authorization") String token){
        boolean success = commodityService.updateComodity(commodity, token);

        if(!success) throw new DefaultException(Code.ERROR, "修改商品信息失败");

        return R.ok().code(Code.SUCCESS).message("添加商品成功");
    }

    @PostMapping("getCommodityList")
    public R getCommodityListByCategoryId(@RequestBody StoreCategories storeCategories, PageUtill pageUtill){

        Page commodityList = commodityService.getCommodityListByCategoryId(storeCategories, pageUtill);
       return R.ok().code(Code.SUCCESS).data("commodityList", commodityList).message("获取成功");
    }

    @PostMapping("getCommodityListByUserId")
    public R getCommodityListByUserId(@RequestBody Page page, @RequestHeader("Authorization") String token){
        Page commodityList = commodityService.getCommodityListByUserId(page, token);

        return R.ok().code(Code.SUCCESS).data("commodityList", commodityList).message("获取成功");
    }

    @DeleteMapping("delCommodity")
    public R delCommodity(@RequestBody  Commodity commodity, @RequestHeader("Authorization") String token){

        boolean success = commodityService.delCommodity(commodity, token);

        if(!success) throw new DefaultException(Code.ERROR, "删除商品失败");

        return R.ok().code(Code.SUCCESS).message("删除商品成功");
    }
}
